'''
Created on 2013/02/26

@author: sakurai
'''
from pygitlabapi import ApiMethodCollectionBase

class Users(ApiMethodCollectionBase):
  @classmethod
  def get_users(cls, api):
    cls._validate_arguments(api)
    params = {"private_token": api.private_token}
    return api._do_GET_with_pagination(api.api_base+"/users", params=params)

  @classmethod
  def get_single_user(cls, api, user_id):
    cls._validate_arguments(api)
    cls._validate_arguments_not_None(user_id)
    params = {"private_token": api.private_token}
    return api._do_GET(api.api_base+"/users/%s"%user_id, params=params)

  @classmethod
  def create_user(cls, api, email, password, username, name, skype=None, linkedin=None, twitter=None, project_limit=10, extern_uid=None, provider=None, bio=None):
    cls._validate_arguments(api)
    params = {
      "email": email,
      "password": password,
      "username": username,
      "name": name,
    }
    cls._validate_arguments_not_None(params)
    params["private_token"] = api.private_token
    if skype: params["skype"]=skype
    if linkedin: params["linkedin"]=linkedin
    if twitter: params["twitter"]=twitter
    if project_limit: params["project_limit"]=project_limit
    if extern_uid: params["extern_uid"]=extern_uid
    if provider: params["provider"]=provider
    if bio: params["bio"]=bio
    return api._do_POST(api.api_base+"/users", params)
    
  @classmethod
  def modify_user(cls, api, user_id, email=None, password=None, username=None, name=None, skype=None, linkedin=None, twitter=None, project_limit=10, extern_uid=None, provider=None, bio=None):
    cls._validate_arguments(api)
    cls._validate_arguments_not_None(user_id)
    params = {"private_token": api.private_token}
    if email: params["email"]=email
    if password: params["password"]=password
    if username: params["username"]=username
    if name: params["name"]=name
    if skype: params["skype"]=skype
    if linkedin: params["linkedin"]=linkedin
    if twitter: params["twitter"]=twitter
    if project_limit: params["project_limit"]=project_limit
    if extern_uid: params["extern_uid"]=extern_uid
    if provider: params["provider"]=provider
    if bio: params["bio"]=bio
    return api._do_PUT(api.api_base+"/users/%s"%user_id, params)

  @classmethod
  def delete_user(cls, api, user_id):
    cls._validate_arguments(api)
    cls._validate_arguments_not_None(user_id)
    params = {"private_token": api.private_token}
    return api._do_DELETE(api.api_base+"/users/%s"%user_id, params=params)

  @classmethod
  def get_current_user(cls, api):
    cls._validate_arguments(api)
    params = {"private_token": api.private_token}
    return api._do_GET(api.api_base+"/user", params=params)

  @classmethod
  def get_ssh_keys(cls, api):
    cls._validate_arguments(api)
    params = {"private_token": api.private_token}
    return api._do_GET_with_pagination(api.api_base+"/user/keys", params=params)

  @classmethod
  def get_single_ssh_key(cls, api, key_id):
    cls._validate_arguments(api)
    cls._validate_arguments_not_None(key_id)
    params = {"private_token": api.private_token}
    return api._do_GET(api.api_base+"/user/keys/%s"%key_id, params=params)

  @classmethod
  def add_ssh_key(cls, api, title, key):
    cls._validate_arguments(api)
    params = {
      "title": title,
      "key": key,
    }
    cls._validate_arguments_not_None(params)
    params["private_token"] = api.private_token
    return api._do_POST(api.api_base+"/user/keys", params)

  @classmethod
  def delete_ssh_key(cls, api, key_id):
    cls._validate_arguments(api)
    cls._validate_arguments_not_None(key_id)
    params = {"private_token": api.private_token}
    return api._do_DELETE(api.api_base+"/user/keys/%s"%key_id, params=params)

